<?php

namespace app\api\controller;
use app\admin\model\ArticleCollect;
use app\admin\model\AskingBack;
use app\admin\model\BrowseLog;
use app\admin\model\Comment;
use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Article extends Api
{
    protected $noNeedLogin = ['articleList','detail','areaArticleList','articleDetail','commentList'];
    protected $noNeedRight = ['*'];
    protected $noNeedSite = ['*'];
    protected $noNeedCheck = ['*'];
    protected $groupCategrory = [2=>48,3=>50,4=>49,5=>47,6=>51];

    /**
     * Notes: 文章-列表
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/26 0026
     * Time: 15:58
     */
    public function articleList(){
        $site_id = input('site_id');
        $keywords = input('keywords');
        $answered = input('answered');
        $limit = input('limit');
        $hot = input('hot');
        $order = "createtime desc";
        $where=[];
        if(!empty($keywords)){
            $where['title']=['like','%'.$keywords.'%'];
        }
        if($answered==1){
            $where['comment'] = 0;
        }
        if($hot){
            $order = "comment desc createtime desc";
        }
        if(!empty($site_id)){
            $where['site_id'] = $site_id;
        }else{
            $where['site_id'] = 0;
        }
        $field="*";
        $askingModel = new \app\common\model\Article();
        $askingData = $askingModel->_list($field,$where,$limit,$order);
        $this->success('成功',$askingData);
    }

    public function areaArticleList(){
        $site_id = input('site_id',0);
        $category_id = input('category_id');
        if(!isInt($site_id,1) || empty($category_id))$this->error('缺少参数');
        $limit = input('limit');
        $order = "createtime desc";
        $where=[];
        $where['site_id'] = $site_id;
        $where['category_id'] = $category_id;
        $where['publishdata'] = '2';
        $where['status'] = '1';
        $field="*";
        $askingModel = new \app\common\model\Article();
        $askingData = $askingModel->_list($field,$where,$limit,$order);
        $this->success('成功',$askingData);
    }

    /**
     * Notes: 文章管理模块列表
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/9 0009
     * Time: 17:06
     * @throws \think\exception\DbException
     */
    public function articleManageList(){
        $site_id = input('site_id');
        $user_id = $this->user_id;;
        $keywords = input('keywords');
        $is_my = input('is_my',0);
        $limit = input('limit');
        $order = "createtime desc";
        $userInfo = Db::name('user')->where('id',$user_id)->find();
        $where=[];
        $category_id = [];
        if(!empty($keywords)){
            $where['title']=['like','%'.$keywords.'%'];
        }
        if(!empty($site_id)){
            $where['site_id'] = $site_id;
        }
        if(in_array((int)$userInfo['group_id'],[2,3,4,5,6])){
            $a = $this->groupCategrory;
            $category_id[] = $a[$userInfo['group_id']];
        }elseif($userInfo['group_id']==7){
            $category_id =[47,48,49,50,51];
        }
        if($category_id){
            $b = getLowestCategory($category_id);
            $category_id = array_merge($category_id,$b);
            $where['category_id'] = ['in',$category_id];
        }
        if($is_my){
            $where['user_id'] = $user_id;
        }
        $where['status'] = '1';
        $field="*";
        $askingModel = new \app\common\model\Article();
        $askingData = $askingModel
            ->field($field)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $this->success('成功',$askingData);
    }

    /**
     * Notes: 隐藏文章
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/8 0008
     * Time: 17:25
     */
    public function hide(){
        $site_id = input('site_id');
        $article_id = input('article_id');
        $publishdata = input('publishdata');
        if(empty($article_id) || empty($publishdata))$this->error('缺少参数');
        $updateData['publishdata']=$publishdata;
        if($publishdata=='2'){
            $updateData['publishtime']=time();
        }
        $res = Db::name('article')->where('site_id',$site_id)->where('id',$article_id)->update($updateData);
        if($res)$this->success('操作成功');
        $this->error('操作失败');
    }

    /**
     * Notes: 软删除文章
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/8 0008
     * Time: 17:27
     */
    public function delete(){
        $site_id = input('site_id');
        $article_id = input('article_id');
        $status = input('status',0);
        if(empty($article_id))$this->error('缺少参数');
        $res = Db::name('article')->where('site_id',$site_id)->where('id',$article_id)->setField('status',$status);
        if($res)$this->success('操作成功');
        $this->error('操作失败');
    }

    /**
     * 读取文章分类数据,联动列表
     */
    public function category()
    {
        $pid = $this->request->param('pid','');
        $user_id = $this->request->param('user_id');
        $site_id = $this->request->param('site_id',0);
        $where = ['status' => 'normal'];
        $where['type'] = 'article';
        $userInfo = Db::name('user')->where('id',$user_id)->find();
        if($pid==46){
            switch ((int)$userInfo['group_id']){
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                    $where['id'] = $this->groupCategrory[$userInfo['group_id']];
                    break;
            }
        }
        $categorylist = null;
        if ($pid !== '') {
            if(!$site_id)$this->error('请先选择站点');
            $where['site_id'] = $site_id;
            $where['pid'] = $pid;
        }else{
            $where['id'] = ['in',[25,46]];
        }
        $categorylist = Db::name('category')->where($where)->field('id ,name')->order('weigh desc,id desc')->select();
        $this->success('成功', $categorylist);
    }

    /**
     * Notes: 添加文章
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/9 0009
     * Time: 10:11
     */
    public function add(){
        $param = $this->request->param();
        $validate = new \app\api\validate\Article();
        if (!$validate->scene('add')->check($param)) {
            $this->error($validate->getError());
        }
        $articleModel = new \app\admin\model\Article();
        if(!empty($param['second'])){
            $param['category_id'] = $param['second'];
        }else{
            $param['category_id'] = $param['first'];
        }
        $param['publishtime'] = time();
        $param['postcontent'] = $_POST['postcontent'];
        $res = $articleModel->allowField(true)->save($param);
        if($res)$this->success('操作成功');
        $this->error('操作失败');
    }

    /**
     * Notes: 编辑文章
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/9 0009
     * Time: 11:43
     */
    public function edit(){
        $param = $this->request->param();
        $validate = new \app\api\validate\Article();
        if (!$validate->scene('edit')->check($param)) {
            $this->error($validate->getError());
        }
        $articleModel = new \app\admin\model\Article();
        if(!empty($param['second'])){
            $param['category_id'] = $param['second'];
        }else{
            $param['category_id'] = $param['first'];
        }
        if($param['postcontent'])$param['postcontent'] = $_POST['postcontent'];
        $res = $articleModel->allowField(true)->save($param,['id'=>$param['id']]);
        if($res)$this->success('操作成功');
        $this->error('操作失败');
    }

    /**
     * Notes: 向上级推送文章
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/9 0009
     * Time: 16:27
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function push(){
        $article_id = input('article_id');
        $user_id = $this->user_id;
        $userInfo = Db::name('user')->where('id',$user_id)->find();
        if(empty($article_id))$this->error('缺少参数');
        if(!$userInfo['is_siter'])$this->error('无此权限');
        $parent_site_id = getParentSite($user_id);
        if(empty($parent_site_id) || $parent_site_id==$user_id)$this->error('无上级站点');
        $articleModel = new \app\admin\model\Article();
        $articleInfo = Db::name('article')->where('id',$article_id)->find();
        if(!$articleInfo)$this->error('无此文章');
        unset($articleInfo['id']);
        $articleInfo['user_id'] = $parent_site_id;
        $articleInfo['site_id'] = $parent_site_id;
        $articleInfo['from_site_id'] = $user_id;
        $articleInfo['publishdata'] = '1';
        if($articleModel->allowField(true)->save($articleInfo)){
            Db::name('article')->where('id',$article_id)->setField('push',1);
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

    /**
     * Notes: 文章收藏列表
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/9 0009
     * Time: 17:32
     * @throws \think\exception\DbException
     */
    public function collectList(){
        $user_id = $this->request->param('user_id');
        $limit = $this->request->param('limit',15);
        $articleCollectModel = new ArticleCollect();
        $collectArticle = $articleCollectModel
            ->field('a.id as article_id,a.title,ac.createtime,u.nickname')
            ->alias('ac')
            ->join('user u','u.id=ac.user_id')
            ->join('article a','ac.article_id=a.id')
            ->where('ac.user_id',$user_id)
            ->paginate($limit);
        $this->success('成功',$collectArticle);
    }

    /**
     * Notes: 收藏文章
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/9 0009
     * Time: 16:28
     */
    public function collect(){
        $param = $this->request->param();
        if(!isset($param['article_id']) || empty($param['article_id']))$this->error('文章id不可为空');
        $data['user_id'] = $param['user_id'];
        $data['article_id'] = $param['article_id'];
        $articleCollectModel = new ArticleCollect();
        $collect = $articleCollectModel->where('article_id',$param['article_id'])->where('user_id',$param['user_id'])->find();
        if($collect)$this->error('你已收藏该文章');
        if($articleCollectModel->save($data))$this->success('操作成功');
        $this->error('操作失败');
    }

    /**
     * Notes: 取消收藏
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/9 0009
     * Time: 17:18
     */
    public function cancelCollect(){
        $param = $this->request->param();
        if(!isset($param['article_id']) || empty($param['article_id']))$this->error('文章id不可为空');
        $articleCollectModel = new ArticleCollect();
        $res = $articleCollectModel->where('article_id',$param['article_id'])->where('user_id',$param['user_id'])->delete();
        if($res)$this->success('操作成功');
        $this->error('操作失败');
    }

    /**
     * Notes: 浏览记录
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/9 0009
     * Time: 17:35
     * @throws \think\exception\DbException
     */
    public function browseLog(){
        $user_id = $this->request->param('user_id');
        $limit = $this->request->param('limit',15);
        $browseLogModel = new BrowseLog();
        $where['hawk_browse_log.user_id'] = $user_id;
        $browseLog = $browseLogModel->_list($where,'createtime desc',$limit);
        $this->success('成功',$browseLog);
    }

    /**
     * Notes: 清空浏览记录
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/9 0009
     * Time: 17:38
     */
    public function browseLogDelete(){
        $user_id = $this->request->param('user_id');
        $browseLogModel = new BrowseLog();
        $res = $browseLogModel->where('user_id',$user_id)->delete();
        if($res)$this->success('操作成功');
        $this->error('操作失败');
    }

    /**
     * Notes: 评论记录
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/10 0010
     * Time: 15:39
     */
    public function comment(){
        $user_id = $this->user_id;;
        $limit = input('limit',15);
        $commentModel = new Comment();
        $field = "c.*,u.username,u.avatar,a.title";
        $comment = $commentModel
            ->alias('c')
            ->field($field)
            ->join('user u','u.id=c.user_id')
            ->join('article a','a.id=c.article_id')
            ->where('c.site_id',$user_id)
            ->where('c.status','1')
            ->paginate($limit);
        $this->success('成功',$comment);
    }

    /**
     * Notes: 评论记录
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/10 0010
     * Time: 15:39
     */
    public function commentList(){
        $article_id = input('article_id');
        $limit = input('limit',15);
        $commentModel = new \app\common\model\Comment();
        //$where['status'] = '1';
        $where['article_id'] = $article_id;
        $comment = $commentModel
            ->with(['user'])
            ->where($where)
            ->order('createtime desc')
            ->paginate($limit);
        $this->success('成功',$comment);
    }

    /**
     * Notes: 评论发布
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/10 0010
     * Time: 16:04
     */
    public function commentAdd(){
        $user_id = $this->user_id;;
        $article_id = input('article_id');
        $content = input('content');
        if(empty($article_id) || empty($content))$this->error('缺少参数');
        $site_id = Db::name('article')->where('id',$article_id)->value('site_id');
        $data = [
            'user_id'=>$user_id,
            'site_id'=>$site_id,
            'article_id'=>$article_id,
            'content'=>$content
        ];
        $commentModel = new Comment();
        Db::name('article')->where('id',$article_id)->setInc('comment');
        if($commentModel->save($data))$this->success('操作成功');
        $this->error('操作失败');
    }

    /**
     * Notes: 删除评论
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/10 0010
     * Time: 16:07
     */
    public function commentHide(){
        $comment_id = input('comment_id');
        if(empty($comment_id))$this->error('缺少参数');
        $commentModel = new Comment();
        $commentInfo = $commentModel->where('id',$comment_id)->find();
        $res = $commentModel->where('id',$comment_id)->delete();
        Db::name('article')->where('id',$commentInfo['article_id'])->setDec('comment');
        if($res)$this->success('操作成功');
        $this->error('操作失败');
    }

    /**
     * Notes: 文章详情
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/19 0019
     * Time: 17:15
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function articleDetail(){
        $article_id = input('article_id');
        if(empty($article_id))$this->error('缺少参数');
        $articleModel = new \app\admin\model\Article();
        $detail = $articleModel->where('id',$article_id)->where('status','1')->find();
        $this->success('成功',$detail);
    }
}